//
//  HealthReportViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 16/9/21.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "HealthReportViewController.h"
#import "HealthStatisticsViewController.h"
#import "DetailHealthReportsViewController.h"
#import "CLUIBaseTableViewController+Empty.h"
#import "NSString+Helper.h"

@interface HealthReportViewController ()
@property (strong, nonatomic) NSMutableArray *arrDataSource;
@end

@implementation HealthReportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 50;
    self.title = @"健康报告";
    [self showNavgationWithLeftItemWithTitle:@"首页"];
    
    NSMutableArray *arr = [NSMutableArray array];
    NSDateFormatter *showFormatter = [[NSDateFormatter alloc]init];
    [showFormatter setDateFormat:@"yyyy'年' '第'w'周'"];
    NSDateFormatter  *formatter=[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *activationDate = [formatter dateFromString:[[NSUserDefaults standardUserDefaults] objectForKey:ActivationDate]];
    NSDate *todyDate = [NSDate date];
    NSInteger interval = [self numberOfDaysWithFromDate:activationDate toDate:todyDate];
    for (int i = 1; i< interval; i++) {
        NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*i sinceDate:todyDate];
        NSString *locationString=[showFormatter stringFromDate:lastDay];
        [arr addObject:locationString];
    }
    //移除最后一周
    for (NSString *str in arr) {
        if (![self.arrDataSource containsObject:str]) {
            [self.arrDataSource addObject:str];
        }
    }
    [self.arrDataSource removeObject:self.arrDataSource.firstObject];
    if(self.arrDataSource.count == 0){
    [self showLabelEmptyCate:CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT -140 -64)/2, 100, 100) withTitle:@"这里还没有内容" withImage:[UIImage imageNamed:@"icon_ blank"]];
    }
}

- (NSInteger)numberOfDaysWithFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comp = [calendar components:NSCalendarUnitDay
                                         fromDate:fromDate
                                           toDate:toDate
                                          options:NSCalendarWrapComponents];
    return comp.day;
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    NSString *title = _arrDataSource[indexPath.row];
    NSDateFormatter *showFormatter = [[NSDateFormatter alloc]init];
    [showFormatter setDateFormat:@"yyyy'年' '第'w'周'"];
    NSDate *currentDate = [showFormatter dateFromString:title];
    NSDateFormatter  *formatter=[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *lastDay = [NSDate dateWithTimeInterval:24*60*60 sinceDate:currentDate];
    NSString *locationString=[formatter stringFromDate:lastDay];
    NSString *dateString =[NSString stringWithFormat:@"%@",_arrDataSource[indexPath.row]];
    NSArray *array = [_arrDataSource[indexPath.row]componentsSeparatedByString:@" "];
    NSString *year = array[0];
    NSString *str =  [self getAfterSevenDaysWithStartDate:locationString andWeekString:dateString];
    NSString *str1 = [NSString stringWithFormat:@"%@ %@",year,str];
    cell.textLabel.text = str1;
    cell.textLabel.font = [UIFont systemFontOfSize:17.0f];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *title = _arrDataSource[indexPath.row];
    NSDateFormatter *showFormatter = [[NSDateFormatter alloc]init];
    [showFormatter setDateFormat:@"yyyy'年' '第'w'周'"];
    NSDate *currentDate = [showFormatter dateFromString:title];
    NSDateFormatter  *formatter=[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];

    NSDate *lastDay = [NSDate dateWithTimeInterval:24*60*60 sinceDate:currentDate];
    NSString *locationString=[formatter stringFromDate:lastDay];
    NSString *dateString =[NSString stringWithFormat:@"%@",_arrDataSource[indexPath.row]];
    HealthStatisticsViewController *healthStatistics = [[UIStoryboard storyboardWithName:@"HealthReportViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"HealthStatisticsViewController"];
    healthStatistics.startDateString = locationString;
    healthStatistics.strTitle = dateString;
    healthStatistics.title = [self getAfterSevenDaysWithStartDate:locationString andWeekString:dateString];
    [self.navigationController pushViewController:healthStatistics animated:YES];
    /*
    DetailHealthReportsViewController *detailReport = [[DetailHealthReportsViewController alloc]init];
    [self.navigationController pushViewController:detailReport animated:YES];
    detailReport.startDateString = locationString;
    detailReport.title = [self getAfterSevenDaysWithStartDate:locationString andWeekString:dateString];*/
}

#pragma mark set/ger
- (NSMutableArray *)arrDataSource{
    if (_arrDataSource == nil) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}

#pragma 通过开始日期和周 拼接标题栏字符串
- (NSString *)getAfterSevenDaysWithStartDate:(NSString *)startDate andWeekString:(NSString *)week
{
    NSString *str = nil;
    NSString *str1 = nil;
    NSString *str2 = nil;
    NSMutableArray *arrDares =[NSMutableArray array];
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"YYYY-MM-dd"];
    NSDate* date = [formater dateFromString:startDate];
    for (int i = 0; i < 7; i++) {
        NSDate *lastDay = [NSDate dateWithTimeInterval:24*60*60*i sinceDate:date];
        NSString * locationString = [formater stringFromDate:lastDay];
        NSArray *arr = [locationString componentsSeparatedByString:@"-"];
        [arrDares addObject:[NSString stringWithFormat:@"%@.%@",arr[1],arr[2]]];
        str1 = [NSString stringWithFormat:@"(%@-%@)",arrDares.firstObject,arrDares.lastObject];
    }
    NSArray *array = [week componentsSeparatedByString:@" "];
    str2 = [array lastObject];
    str = [NSString stringWithFormat:@"%@  %@",str2,str1];
    return str ;
}


@end
